/********************************** (C) COPYRIGHT *********************************
 * File Name          : CH392INC.H
 * Author             : WCH
 * Version            : V1.1
 * Date               : 2022/12/28
 * Description        : CH392 Command definition specification
 **********************************************************************************/
#ifndef __CH392INC_H__
#define __CH392INC_H__
#ifdef __cplusplus
extern "C"
{
#endif

//#define CH392T
#define CH392F

#define CH392_UART_MODE 1
#define CH392_SPI_MODE 2

#define CH392_OP_INTERFACE_MODE CH392_UART_MODE

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif

#ifndef UINT8
   typedef unsigned char UINT8;
#endif
#ifndef UINT16
   typedef unsigned short UINT16;
#endif
#ifndef UINT32
   typedef unsigned long UINT32;
#endif
#ifndef INT8
   typedef signed char INT8;
#endif
#ifndef INT16
   typedef signed short INT16;
#endif
#ifndef INT32
   typedef signed long INT32;
#endif
#ifndef UINT8V
   typedef unsigned char volatile UINT8V;
#endif
#ifndef PUINT8
   typedef unsigned char *PUINT8;
#endif
#ifndef PUINT16
   typedef unsigned short *PUINT16;
#endif
#ifndef PUINT32
   typedef unsigned long *PUINT32;
#endif
#ifndef PUINT8V
   typedef unsigned char volatile *PUINT8V;
#endif

/* ********************************************************************************************************************* */
/* Cmd Code */
/* A command operation sequence includes:
          A command code (for serial port mode, two synchronization codes are required before the command code),
          Several input data (can be 0),
          Several output data (can be 0)
       Rule for naming command codes: CMDxy_NAME
       x indicates the minimum number of input data (bytes), y indicates the minimum number of output data (bytes).
       y If it is W, Indicates that you need to wait until the command is successfully executed
       Some commands can read and write data blocks of 0 to more than one byte,
       the number of bytes of the data block itself is not included in the x or y above
/* ********************************************************************************************************************* */
#define CMD01_GET_IC_VER 0x01 /* Obtain the chip and firmware version number */
   /* Output: Version number*/

#define CMD31_SET_BAUDRATE 0x02 /* Serial port: Set the baudrate for serial port communication (The default baudrate after power-on or reset is 9600bps */
   /* Input: 3 bytes baud rate, low bytes first */
   /* Output: operation status (CMD_RET_SUCCESS, other values indicate operation incomplete) */

#define CMD00_RESET_ALL 0x05 /* Perform hardware reset */

#define CMD11_CHECK_EXIST 0x06 /* Test communication interface and working condition */
   /* Input: Any data */
   /* Output: bitwise inverse of input data*/

#define CMD02_GET_GLOB_INT_STATUS_ALL 0x19 /* Gets the CH392T global interrupt status  */
   /* Output: Global interrupt status, refer to Global interrupt status definition */

#define CMD60_SET_MAC_ADDR 0x21 /* Set MAC address , Must be set before CMD00H_INIT_CH392 */
   /* Input: 6-byte MAC address */

#define CMD40_SET_IP_ADDR 0x22 /* Set IP address , Must be set before CMD00H_INIT_CH392  */
   /* Input: 4-byte IP address */

#define CMD40_SET_GWIP_ADDR 0x23 /* Set GWIP address , Must be set before CMD00H_INIT_CH392 */
   /* Input: 4-byte GWIP address */

#define CMD40_SET_MASK_ADDR 0x24 /* Set MASK address , Must be set before CMD00H_INIT_CH392 */
   /* Input: 4-byte GWIP address */

#define CMD01_GET_PHY_STATUS 0x26 /* Gets the current PHY status, such as disconnected, FULL/HALF */
   /* Output: Current PHY status. See PHY Parameter definition for the status definition */

#define CMD0W_INIT_CH392 0x27 /* Initialize CH392 */
   /* This command takes about 200MS to run. This command needs to wait for the command to execute successfully */

#define CMD08_GET_UNREACH_IPPORT 0x28 /* Get unreachable information */
   /* Output: 8 bytes, the first byte is the unreachable type, refer to the unreachable type definition */
   /*         The second byte is the inaccessible protocol code */
   /*         The third and fourth bytes are unreachable ports */
   /*         Bytes 5-8 are unreachable IP */

#define CMD01_GET_GLOB_INT_STATUS 0x29 /* Gets the CH392F global interrupt status */
   /* Output: Global interrupt status, refer to Global interrupt status definition */

#define CMD01_GET_CMD_STATUS 0x2C /* Obtain the command execution status */
   /* Output: Command execution status, refer to the command execution status definition */

#define CMD06_GET_REMOT_IPP_SN 0x2D /* Obtain the port and IP address of the remote end. This command is used in TCP server mode */
   /* Output: 6 bytes. Bytes 1-4 are the IP address of the remote end, and bytes 5-6 are the port number of the remote end */

#define CMD10_CLEAR_RECV_BUF_SN 0x2E /* Clear receive buffer  */
   /* Input: The first byte is the index of the socket */

#define CMD12_GET_SOCKET_STATUS_SN 0x2F /* Obtain the socket n status*/
   /* Input:  the index of the socket*/
   /* Output: First byte: socket n open or closed
              Second byte: TCP state, meaningful only if TCP mode and the first byte is open */

#define CMD11_GET_INT_STATUS_SN 0x30 /* Gets the interrupt status of socket n */
   /* Input: index of the socket */
   /* Output: socket interrupt status, refer to Global interrupt status definition */

#define CMD50_SET_IP_ADDR_SN 0x31 /* Set the destination IP address of socket n */
   /* Input: The first byte is the index value of the socket, Bytes 2-5 are the IP address */

#define CMD30_SET_DES_PORT_SN 0x32 /* Set the destination port of socket n */
   /* Input: The first byte is the index value of the socket, Bytes 2-3 are the destination port */

#define CMD30_SET_SOUR_PORT_SN 0x33 /* Set the source port of socket n */
   /* Input: The first byte is the index value of the socket, Bytes 2-3 are the source port */

#define CMD20_SET_PROTO_TYPE_SN 0x34 /* Set the protocol type of socket n */
   /* Input: The first byte is the index value of the socket, the second protocol type, refer to the protocol type definition */

#define CMD1W_OPEN_SOCKET_SN 0x35 /* Open socket n */
   /* Input: The first byte is the index value of the socket. This command needs to wait for the command to execute successfully */

#define CMD1W_TCP_LISTEN_SN 0x36 /* socket n listens, After receiving this command, socket n enters server mode, valid only for TCP mode */
   /* Input: The first byte is the index value of the socket. This command needs to wait for the command to execute successfully */

#define CMD1W_TCP_CONNECT_SN 0x37 /* socket n connection. After receiving this command, socket n enters the client mode, valid only for TCP mode */
   /* Input: The first byte is the index value of the socket. This command needs to wait for the command to execute successfully */

#define CMD1W_TCP_DISNCONNECT_SN 0x38 /* socket n disconnection, valid only for TCP mode */
   /* Input: The first byte is the index value of the socket. */

#define CMD30_WRITE_SEND_BUF_SN 0x39 /* Writes data to socket n buffer */
   /* Input: The first byte is the index value of the socket, and the second to third bytes is the length ,And a certain byte of data*/

#define CMD12_GET_RECV_LEN_SN 0x3B /* Gets the length of data received by socket n */
   /* Input: index of the socket */
   /* Output: 2 bytes receive length */

#define CMD30_READ_RECV_BUF_SN 0x3C /* Gets socket n receive buffer data */
   /* Input: The first byte is the index value of the socket, and the second to third bytes is the read length n */
   /* Output: n data */

#define CMD1W_CLOSE_SOCKET_SN 0x3D /* Close socket n */
   /* Input: index of the socket */

#define CMD06_GET_MAC_ADDR 0x40 /* Gets the MAC address */
   /* Output: 6-byte MAC address */

#define CMD10_DHCP_ENABLE 0x41 /* Enable DHCP */
   /* Input: 1 byte, 1 enable DHCP, 0 disable DHCP */

#define CMD01_GET_DHCP_STATUS 0x42 /* Obtain DHCP status */
   /* Output: 1 byte status code, 0 indicates success, other values fail */

#define CMD014_GET_IP_INF 0x43 /* Get IP, subnet mask, gateway */
   /* Output: 20 bytes, respectively 4 bytes IP, 4 bytes gateway, 4 bytes mask, 4 bytes DNS1, 4 bytes DNS2 */

#define CMD20_SET_TTL 0x51 /* Set the TTL. The maximum value of the TTL is 128 */
   /* Input: The first byte is the index value of the socket, and the second byte is the TTL value. The maximum value is 128 */

#define CMD30_SET_RECV_BUF 0x52 /* Sets the SOCKET receive buffer */
   /* Input: the first byte is the index value of the socket, the second byte is the starting block index, and the third byte is the number of blocks */

#define CMD40_SET_KEEP_LIVE_IDLE 0x56 /* Set KEEPLIVE idle time */
   /*Input: 4-byte keepalive idle time parameter, the low order first*/

#define CMD40_SET_KEEP_LIVE_INTVL 0x57 /* Set KEEPLIVE interval time */
   /* Input: 4-byte keepalive timeout interval, with the lowest bit before */

#define CMD10_SET_KEEP_LIVE_CNT 0x58 /* Set KEEPLIVE retries times */
   /* Input: retry times in 1 byte */

#define CMD20_SET_KEEP_LIVE_SN 0X59 /* Set the socket n keeplive function */
   /* Input: 1 byte Socket index, 1 byte setting */

#define CMD01_GET_SEND_QUEUE 0X60 /* Gets the number of available send queues */
   /* Output: Number of free queues in 1 byte */

#define CMD00_SET_MACRAW 0XBA /* MACRAW mode, you can directly send and receive MAC frames*/

/* Protocol type */
#define PROTO_TYPE_UDP 2 /* UDP */
#define PROTO_TYPE_TCP 3 /* TCP */

/* PHY status */
#define PHY_DISCONN (1 << 0)  /* PHY disconnect */
#define PHY_10M_FLL (1 << 1)  /* 10M FULL */
#define PHY_10M_HALF (1 << 2) /* 10M HALF */

/* interrupt status */
/* The following is the state generated by GLOB_INT */
#define GINT_STAT_UNREACH (1 << 0)    /* Unreachable interrupt */
#define GINT_STAT_IP_CONFLI (1 << 1)  /* IP conflict */
#define GINT_STAT_PHY_CHANGE (1 << 2) /* PHY status changes */
#define GINT_STAT_DHCP (1 << 3)       /* DHCP status change */
#define GINT_STAT_SOCK0 (1 << 4)      /* socket0 interrupt */
#define GINT_STAT_SOCK1 (1 << 5)      /* socket1 interrupt */
#define GINT_STAT_SOCK2 (1 << 6)      /* socket2 interrupt */
#define GINT_STAT_SOCK3 (1 << 7)      /* socket3 interrupt */

#ifdef CH392T /*The 392T uses eight sockets by default and a maximum of 32 sockets. For details, see the 392 manual*/

#define GINT_STAT_SOCK4 (1 << 8)      /* socket4 interrupt */
#define GINT_STAT_SOCK5 (1 << 9)      /* socket5 interrupt */
#define GINT_STAT_SOCK6 (1 << 10)     /* socket6 interrupt */
#define GINT_STAT_SOCK7 (1 << 11)     /* socket7 interrupt */
#define GINT_STAT_SOCK8 (1 << 12)     /* socket8 interrupt */
#define GINT_STAT_SOCK9 (1 << 13)     /* socket9 interrupt */
#define GINT_STAT_OTHERSOCK (1 << 14) /* other socket interrupt */
#endif

/* The following are the states generated by Sn_INT */
#define SINT_STAT_SENBUF_FREE (1 << 0) /* Send buffer free */
#define SINT_STAT_SEND_OK (1 << 1)     /* send successfully */
#define SINT_STAT_RECV (1 << 2)        /* The socket port received data or the receive buffer was not empty */
#define SINT_STAT_CONNECT (1 << 3)     /* successfu connect,This interrupt is generated in TCP mode */
#define SINT_STAT_DISCONNECT (1 << 4)  /* disconnect,This interrupt is generated in TCP mode */
#define SINT_STAT_TIM_OUT (1 << 6)     /* This interrupt occurs in TCP mode */

/* Get command status */
#define CMD_ERR_SUCCESS 0x00  /* Command operation succeeded */
#define CMD_RET_ABORT 0x5F    /* Command operation failure */
#define CH392_ERR_BUSY 0x10   /* Busy: indicates that the command is being executed */
#define CH392_ERR_UNKNOW 0xFA /* Unknown error */

/* unreachable code */
#define UNREACH_CODE_HOST 0     /* Host unreachable */
#define UNREACH_CODE_NET 1      /* Network unreachable */
#define UNREACH_CODE_PROTOCOL 2 /* Protocol unreachable */
#define UNREACH_CODE_PROT 3     /* Prot Unreachbale */
/*Refer to the RFC792 document for other values*/

/* Command header */
#define SER_SYNC_CODE1 0x57 /* Serial port command synchronization code 1 */
#define SER_SYNC_CODE2 0xAB /* Serial port command synchronization code 2 */

/* tcp state  */
#define TCP_CLOSED 0
#define TCP_LISTEN 1
#define TCP_SYN_SENT 2
#define TCP_SYN_RCVD 3
#define TCP_ESTABLISHED 4
#define TCP_FIN_WAIT_1 5
#define TCP_FIN_WAIT_2 6
#define TCP_CLOSE_WAIT 7
#define TCP_CLOSING 8
#define TCP_LAST_ACK 9
#define TCP_TIME_WAIT 10

#ifdef __cplusplus
}
#endif
#endif

/**************************** endfile *************************************/
